package dialog;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;

import main.Game;
import model.Bank;
import model.Board;
import model.Cell;
import model.City;
import view.Card_view;
import controler.BuyHouses;
import controler.NbHouseListener;

public class HouseDialog extends JDialog{

	City c;
	Game game;
	public JPanel family, family2;
	ArrayList<City>group;
	int total;
	JPanel enter,validate;
	public JPanel achat,vente;
	JButton confirm,sell,cancel,cancel2;
	public JSlider slider1, slider2, slider3, slider4, slider5, slider6;
	public JTabbedPane onglet;
	JComboBox<String>famille, famille2;
	JLabel lab,lab1;
	Font font = new Font("Arial",Font.BOLD,35);
	GridBagConstraints gb;
	Card_view cv;
	Bank bank;

	//constructor
	public HouseDialog(JFrame parent, String title, boolean modal, Game g,Bank bank){
		super(parent,title,modal);
		this.setSize(1100,800);
		this.getContentPane().setLayout(new GridBagLayout());
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.game=g;
		this.bank=bank;
		this.c=new City();
		initComponent();
		this.setVisible(true);

	}

	private void initComponent(){
		onglet = new JTabbedPane();
		onglet.setSize(1100,800);
		vente = new JPanel();
		achat = new JPanel();
		lab = new JLabel("Building purchase");
		lab.setFont(font);
		lab1 = new JLabel("Building sale");
		lab1.setFont(font);
		//refresh the family's list
		String[]items = new String[game.actual_player.have_full_family().size()];
		items = game.actual_player.have_full_family().toArray(items);
		famille = new JComboBox<String>(items);
		famille2 = new JComboBox<String>(items);
		DefaultListCellRenderer centr = new DefaultListCellRenderer();
		centr.setHorizontalAlignment(JLabel.CENTER);
		famille.setRenderer(centr);
		famille2.setRenderer(centr);
		family=new JPanel();
		family.add(buildFamily(""+famille.getSelectedItem()));
		family2=new JPanel();
		family2.add(buildSellFamily(""+famille.getSelectedItem()));
		famille.addActionListener(new BuyHouses(this));//action listener on the combobox
		famille2.addActionListener(new BuyHouses(this));
		enter = new JPanel();//confirm/cancel for buying
		validate = new JPanel();//confirm/cancel for selling
		confirm = new JButton("Build");
		cancel = new JButton("Cancel");
		cancel2 = new JButton("Cancel");
		sell = new JButton("Sell");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		cancel2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		confirm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(slider1.getValue()==0 && slider2.getValue()==0){
					if(slider3!=null&& slider3.getValue()==0){
						dispose();
					}
					if(slider3==null){
						dispose();
					}
				}

				else  {
					total=0;
					ArrayList<City>family=c.getFamily(""+famille.getSelectedItem());
					total+=(slider1.getValue()-slider1.getMinimum())*family.get(0).house_cost;
					total+=(slider2.getValue()-slider2.getMinimum())*family.get(0).house_cost;
					if(slider3!=null){
						total+=(slider3.getValue()-slider3.getMinimum())*family.get(0).house_cost;
					}
					if(total==0){
						dispose();
					}
					else {
						//transfer to  the purchase.
						canBuy(total);
						dispose();
					}
				}
			}
		});

		sell.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if(slider4.getValue()==0 && slider5.getValue()==0){
					if(slider6!=null&& slider6.getValue()==0){
						dispose();
					}
					if(slider6==null){
						dispose();
					}
				}
				else {
					total=0;
					ArrayList<City>family=c.getFamily(""+famille.getSelectedItem());
					total+=slider4.getValue()*family.get(0).house_cost;
					total+=slider5.getValue()*family.get(0).house_cost;
					if(slider6!=null){
						total+=slider6.getValue()*family.get(0).house_cost;
					}
					if(total==0){
						dispose();
					}
					else canSell(total);
					dispose();
				}
			}
		});
		enter.add(confirm);
		enter.add(cancel);
		validate.add(sell);
		validate.add(cancel2);
		view();
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	}

	public void canBuy(int total){
		//if player can pay
		if(game.actual_player.getAccountBalance()>=total){
			//player pay to the bank the total amount
			bank.payBank(game.actual_player, total);
			//we add the houses on the cells
			for(int i=0;i<group.size();i++){
				if(i==0){
					game.setHouse(group.get(i).name, slider1.getValue());
				}
				else if(i==1){
					game.setHouse(group.get(i).name, slider2.getValue());
				}
				else{
					game.setHouse(group.get(i).name, slider3.getValue());
				}
			}
		}
		//if player can't pay
		else{
			JOptionPane.showMessageDialog(null, "You don't have enough money");
		}
	}
	public void canSell(int total){
		//player pay to the bank the total amount
		bank.give(game.actual_player, total);
		//we add the houses on the cells
		for(int i=0;i<group.size();i++){
			if(i==0){
				game.setHouse(group.get(i).name, slider4.getMaximum()-slider4.getValue());
			}
			else if(i==1){
				game.setHouse(group.get(i).name, slider5.getMaximum()-slider5.getValue());
			}
			else{
				game.setHouse(group.get(i).name, slider6.getMaximum()-slider6.getValue());
			}
		}
	}

	public void view(){
		onglet.removeAll();
		achat.setLayout(new GridBagLayout());
		vente.setLayout(new GridBagLayout());
		gb=new GridBagConstraints();
		gb.insets = new Insets(0, 0, 20, 0);
		gb.gridx=0;
		gb.gridy=0;
		//this.getContentPane().add(lab,gb);//titre
		achat.add(lab,gb);
		vente.add(lab1,gb);
		gb.gridy=1;
		gb.ipadx=20;
		gb.ipady=3;
		//this.getContentPane().add(famille,gb);//combobox
		achat.add(famille,gb);
		vente.add(famille2,gb);
		gb.ipadx=0;
		gb.ipady=0;
		//gb.gridy=2;
		gb.fill=gb.HORIZONTAL;
		gb.gridy=2;
		//this.getContentPane().add(family,gb);//card
		achat.add(family,gb);
		vente.add(family2,gb);
		gb.gridy=3;
		//this.getContentPane().add(enter,gb);//boutons
		achat.add(enter,gb);
		vente.add(validate,gb);
		onglet.add("Buy",achat);
		onglet.add("Sell",vente);
		this.setContentPane(onglet);
	}

	public JPanel buildSellFamily(String name){
		JPanel fam = new JPanel();
		fam.setPreferredSize(new Dimension(1000,520));
		fam.setBorder(BorderFactory.createTitledBorder("Family"));
		fam.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		//we get back the card of the family in a group
		group = c.getFamily(name);
		//for each card of this group, we make the card_view and we add it with a slider
		int j=0;
		gbc.gridy=0;
		gbc.insets=new Insets(0, 10, 0, 10);
		for(int i=0;i<group.size();i++){
			gbc.gridx=j;
			cv = new Card_view(group.get(i));
			fam.add(cv,gbc);
			j++;
			gbc.gridx=j;
			if(i==0){
				this.slider4 = makeSellSlider(this.slider4, group.get(i));
				slider4.addChangeListener(new NbHouseListener(this));
				fam.add(slider4,gbc);
			}
			else if(i==1){
				this.slider5 = makeSellSlider(this.slider5, group.get(i));
				slider5.addChangeListener(new NbHouseListener(this));
				fam.add(slider5,gbc);
			}
			else{
				this.slider6 = makeSellSlider(this.slider6, group.get(i));
				slider6.addChangeListener(new NbHouseListener(this));
				fam.add(slider6,gbc);
			}
			j++;
		}
		return fam;
	}


	public JPanel buildFamily(String name){
		JPanel fam = new JPanel();
		fam.setPreferredSize(new Dimension(1000,520));
		fam.setBorder(BorderFactory.createTitledBorder("Family"));
		fam.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		//we get back the card of the family in a group
		group = c.getFamily(name);
		//for each card of this group, we make the card_view and we add it with a slider
		int j=0;
		gbc.gridy=0;
		gbc.insets=new Insets(0, 10, 0, 10);
		for(int i=0;i<group.size();i++){
			gbc.gridx=j;
			cv = new Card_view(group.get(i));
			fam.add(cv,gbc);
			j++;
			gbc.gridx=j;
			if(i==0){
				this.slider1=makeSlider(this.slider1,group.get(i));
				slider1.addChangeListener(new NbHouseListener(this));
				fam.add(slider1,gbc);
			}
			else if(i==1){
				this.slider2=makeSlider(this.slider2,group.get(i));
				slider2.addChangeListener(new NbHouseListener(this));
				fam.add(slider2,gbc);
			}
			else{
				this.slider3=makeSlider(this.slider3,group.get(i));
				slider3.addChangeListener(new NbHouseListener(this));
				fam.add(slider3,gbc);
			}
			j++;
		}
		return fam;
	}

	private JSlider makeSellSlider(JSlider slider, City c){
		Cell cell=null;
		for(int i=0;i<Board.map.length;i++){
			if(Board.map[i].getName().equals(c.name)){
				cell=Board.map[i];
			}
		}
		slider = new JSlider(JSlider.VERTICAL,0,cell.getNbHouses(),0);
		slider.setName(slider.getName());
		slider.setMajorTickSpacing(1);	
		slider.setPaintTicks(true);
		Hashtable<Integer,JLabel> labelTable = new Hashtable<Integer, JLabel>();
		labelTable.put( new Integer( 0 ), new JLabel("0") );
		labelTable.put( new Integer( 1 ), new JLabel("1") );
		labelTable.put( new Integer( 2 ), new JLabel("2") );
		labelTable.put( new Integer( 3 ), new JLabel("3") );
		labelTable.put( new Integer( 4 ), new JLabel("4") );
		labelTable.put( new Integer( 5 ), new JLabel("Hotel") );
		slider.setLabelTable( labelTable );
		slider.setPaintLabels(true);

		return slider;
	}
	private JSlider makeSlider(JSlider slider, City c){
		Cell cell=null;
		for(int i=0;i<Board.map.length;i++){
			if(Board.map[i].getName().equals(c.name)){
				cell=Board.map[i];
			}
		}
		slider = new JSlider(JSlider.VERTICAL,cell.getNbHouses(),5,cell.getNbHouses());
		slider.setName(slider.getName());
		slider.setMajorTickSpacing(1);	
		slider.setPaintTicks(true);
		Hashtable<Integer,JLabel> labelTable = new Hashtable<Integer, JLabel>();
		labelTable.put( new Integer( 0 ), new JLabel("0") );
		labelTable.put( new Integer( 1 ), new JLabel("1") );
		labelTable.put( new Integer( 2 ), new JLabel("2") );
		labelTable.put( new Integer( 3 ), new JLabel("3") );
		labelTable.put( new Integer( 4 ), new JLabel("4") );
		labelTable.put( new Integer( 5 ), new JLabel("Hotel") );
		slider.setLabelTable( labelTable );
		slider.setPaintLabels(true);

		return slider;
	}
}
